package com.touchdrive;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.touchdrive.NetConnect;

public class SettingsActivity extends Activity{
	CheckBox nvertX;
    CheckBox nvertY;
    EditText deadzoneX;
    EditText deadzoneY;
    EditText ipAddr;
    EditText port;
    Button connect;
    TextView connectStat;
    @Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_settings);
	    restoreSettings();
	    
	}
	

    public void buttonConnect(View view){
    	NetConnect connection=new NetConnect();
    	connection.ipAddr=ipAddr.toString();
    	connection.port=Integer.parseInt(port.getText().toString());
    	connection.connect();
    	if (connection.sock.isConnected()){
    		connectStat.setTextColor(Color.GREEN);
    		connectStat.setText("Connected");
    	}
    }
    
    public void restoreSettings(){
    	SharedPreferences settings = getSharedPreferences("user_prefs", 0);
	    connectStat=(TextView)findViewById(R.id.connectStat);
    	nvertX = (CheckBox)findViewById(R.id.nvertX);
	    nvertY = (CheckBox)findViewById(R.id.nvertY);
	    deadzoneX = (EditText)findViewById(R.id.dzX);
	    deadzoneY = (EditText)findViewById(R.id.dzY);
	    ipAddr = (EditText)findViewById(R.id.ipAddr);
	    port = (EditText)findViewById(R.id.port);
	    connect = (Button)findViewById(R.id.connectButton);
	    nvertX.setChecked(settings.getBoolean("nvertX", false));
	    nvertY.setChecked(settings.getBoolean("nvertY", false));
	    deadzoneX.setText(settings.getString("deadzoneX", "50"));
	    deadzoneY.setText(settings.getString("deadzoneY", "50"));
	    ipAddr.setText(settings.getString("ipAddr", "127.0.0.1"));
	    port.setText(settings.getString("port", "9099"));
    }
    
    public void onStop(){
		SharedPreferences settings = getSharedPreferences("user_prefs", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean("nvertX", nvertX.isChecked());
		editor.putBoolean("nvertY", nvertY.isChecked());
		editor.putString("deadzoneX", deadzoneX.getText().toString());
		editor.putString("deadzoneY", deadzoneY.getText().toString());
		editor.putString("ipAddr", ipAddr.getText().toString());
		editor.putString("port", port.getText().toString());
		editor.commit();
		super.onStop();
	}

}
